﻿using System;
using CardLogistics.Model.Basic;
using CardLogistics.Web.Components;
using System.Web;
using System.IO;

namespace CardLogistics.Web.MainPage.Basic.FormatInfo
{
    public partial class AddEdit : BasePage
    {
        private static readonly CardLogistics.DAL.Basic.Basic_FormatData DAL = new CardLogistics.DAL.Basic.Basic_FormatData();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDLL(ddlSUPPID, "BASIC_SUPPLIERS", "SUPPLIERSNAME", "SUPPLIERSID", " and STATUS=0 and ISDELETE=0");  //供应商
                InitControl();
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public void InitControl()
        {
            if (IsEdit)
            {
                string _key = Request.QueryString["id"];
                if (!string.IsNullOrEmpty(_key))
                {
                    Basic_Format sData = DAL.GetModel(_key);
                    SetWebControls(this.Page, sData);
                    ddlSUPPID.Value = sData.SuppId;//供应商
                    attUrlImg.Src = "~/MainPage/File/Upload/" + sData.ATTURL;
                    attUrlImg.Height = 120;
                    attUrlImg.Width = 180;
                }
            }
        }
        /// <summary>
        /// 保存按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Add_Click(object sender, EventArgs e)
        {
            try
            {
                string imgPath = Server.MapPath("~/MainPage/File/Upload/");
                Basic_Format model = new Basic_Format();
                if (this.file_FormatImage.PostedFile != null && this.file_FormatImage.PostedFile.ContentLength > 0)
                {
                    HttpPostedFile file = this.file_FormatImage.PostedFile;
                    string ext = Path.GetExtension(file.FileName).ToLower();
                    //判断上传照片的格式
                    if (ext != ".gif" && ext != ".jpg" && ext != ".bmp" && ext != ".jpeg" && ext != ".png")
                    {
                        ShowMessageBox("附件类型错误，上传失败!");
                        return;
                    }
                    else
                    {
                        string fileName = string.Format("{0}{1}", DateTime.Now.ToString("yyyyMMddHHmmssfff"), ext);
                        if (!string.IsNullOrEmpty(attUrlImg.Src))
                        {
                            File.Delete(imgPath+(attUrlImg.Src).Split('/')[1].ToString());
                        }

                        file.SaveAs(imgPath + fileName);
                        model = EncapsulationModel();
                        model.ATTURL = fileName;
                    }
                }
                else if (string.IsNullOrEmpty(attUrlImg.Src))
                {
                    ShowMessageBox("请选择附件!");
                    return;
                }
                else
                {
                    model = EncapsulationModel();
                    model.ATTURL = attUrlImg.Src.Replace("~/MainPage/File/Upload/", "");
                }


                string result = "";
                if (IsEdit)
                {
                    result = DAL.Update(model);
                }
                else
                    result = DAL.Add(model);
                if (result.Split(',')[0] == "ok")
                {
                    ReturnListInfo();
                }
                else
                {
                    ShowMessageBox("操作失败请重新操作！" + result.Split(',')[1]);
                }
            }
            catch (Exception ex)
            {
                ShowMessageBox("系统异常:" + ex.ToString());
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private Basic_Format EncapsulationModel()
        {
            Basic_Format model = new Basic_Format();
            model = GetWebControls<Basic_Format>(this.Page);
            model.LASTUPDATEUSER = LoggedUser.ID;
            model.SuppId = ddlSUPPID.Value;//供应商
            if (model.STATUS == null)
                model.STATUS = 0;
            model.ISDELETE = 0;
            if (IsEdit)
                model.FORMATID = Request.QueryString["id"];
            return model;
        }
    }
}